package org.jprobe.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jprobe.utils.UIUtils;
/**
*
* About the jProbe dialog
*
* @author Hussain Al-Mutawa
* @version 1.0
* @since 1.0
*/
@SuppressWarnings("serial")
public class AboutWindow extends JDialog implements ActionListener{
	public  transient static final String PROJECT_URI = "http://code.google.com/p/jprobe/";
	private transient final Container cpane;
	private transient static AboutWindow singleton;
	private transient final JLabel label5;

	/**
	 * initializes new About window object and makes it ready to be displayed
	 */
	public AboutWindow() {
		super();
		JPanel panel0,panel1,panel2,panel3;
		JLabel label1,label3,label4;
		ImageIcon imageIcon;
		JButton button;

		setTitle("About jProbe 1.0");
		cpane=getContentPane();
		setLayout(new BorderLayout());
		panel0=new JPanel();
		panel1=new JPanel();
		panel2=new JPanel();
		panel3=new JPanel();
		button=new JButton("Close");
		imageIcon=UIUtils.createImageIcon( "/./org/jprobe/ui/jprobe.jpg", "jProbe Icon");

		panel0.setLayout(new BorderLayout());
		panel3.setLayout(new BorderLayout());
		panel0.setBackground(Color.WHITE);
		panel1.setBackground(Color.WHITE);
		panel1.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
		panel0.add(panel1,BorderLayout.CENTER);

		cpane.add(panel0,BorderLayout.NORTH);
		cpane.add(panel2,BorderLayout.CENTER);
		panel0.setBorder(BorderFactory.createEtchedBorder());
		panel3.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		label1=new JLabel("<html><div style='padding:5pt'><span style='font-weight:bold;font-size:15pt;color:green'>jProbe</span><br/>1.0<br/>"+
		"<em style='color:gray;font:smaller'>Java project unit testing generator</em></div></html>");
		label3=new JLabel(
				"<html>Copyright &copy; 2011-2012 Hussain Al-Mutawa.<br/>"+
				" All Rights Reserved.<br/><br/>jProbe is made possible by "+
				" the jProbe open source project, please visit us at the"+
				"</html>");
		label5=new JLabel(
				"<html>"+
				" <a style='color:#0000FF' href='"+PROJECT_URI+"'>project web page</a> "+
				" <span style='color:green;font:smaller;white-space:nowrap'>[ "+PROJECT_URI+" ]</span>"+
				"</html>");
		label5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(final MouseEvent mouseEvent) {
				UIUtils.openURI(PROJECT_URI);
				super.mouseClicked(mouseEvent);
			}
			@Override
			public void mousePressed(final MouseEvent mouseEvent) {
				label5.setText(label5.getText().replace("#0000FF", "#993CF3"));
				super.mousePressed(mouseEvent);
			}
			@Override
			public void mouseReleased(final MouseEvent mouseEvent) {
				label5.setText(label5.getText().replace("#993CF3", "#0000FF"));
				super.mousePressed(mouseEvent);
			}
		});
		label4=new JLabel(imageIcon);
		panel0.add(label4,BorderLayout.WEST);
		panel1.setLayout(new BoxLayout(panel1, 1));
		panel2.setLayout(new BorderLayout());
		panel2.add(label3,BorderLayout.CENTER);
		panel2.add(label5,BorderLayout.SOUTH);
		panel3.add(button,BorderLayout.EAST);
		panel2.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		panel1.add(label1);
		cpane.add(panel3,BorderLayout.SOUTH);
		label5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

		button.addActionListener(this);

		setPreferredSize(new Dimension(270,270));
		setSize(getPreferredSize());
		setLocation(UIUtils.getScreenCenterPosition(this));

		setResizable(false);
		setModal(true);
		UIUtils.installEscapeCloseOperation(this);
		pack();
	}

	/**
	 * opens the About dialog
	 */
	public static final AboutWindow open(){
		if(AboutWindow.singleton==null){
			singleton=new AboutWindow();
		}
        singleton.setVisible(true);
		return singleton;
	}


	@Override
	public void actionPerformed(final ActionEvent arg0) {
		this.setVisible(false);
		this.dispose();
	}

}
